import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:flutter_framework/router/index.dart';
import 'package:get/get.dart';
import './controllers/setting_controller.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    TDTheme.needMultiTheme();
    final settingController = Get.put(SettingController());
    return FutureBuilder<String>(
      future: rootBundle.loadString('assets/jsons/theme.json'),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const CircularProgressIndicator(); // 显示加载中的指示器
        } else if (snapshot.hasError) {
          return Text('Error: ${snapshot.error}');
        } else {
          var jsonString = snapshot.data!;
          var themeData = TDThemeData.fromJson('purple', jsonString) ??
              TDThemeData.defaultData();
          return GetMaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(
                extensions: [themeData],
                scaffoldBackgroundColor:
                    settingController.backgroundColor.value,
                primaryColor: settingController.primaryColor.value),
            getPages: RouteConfig.routes,
          );
        }
      },
    );
  }
}
